const ErrorEnum = require("./errorEnum");

class AppError extends Error {
  static instance;

  constructor(statusCode, message, details = {}) {
    super();
    this.status = statusCode;
    this.code = statusCode;
    this.type = ErrorEnum[statusCode].type;
    this.details = details;
    this.message = message;
    Error.captureStackTrace(this, this.constructor);

    return this;
  }
}

function appError(status, message, details) {
  throw new AppError(status, message, details);
}

module.exports = appError;